<?php

namespace App\Http\Controllers;

use App\Http\Resources\BaseResource;
use App\Http\Resources\ErrorResource;
use App\Repositories\Contracts\PolicyUploadInterface;
use App\Repositories\Ota\Ctrip\Policy\Contracts\SpecialPolicyInterface;
use App\Repositories\Ota\Qn\Policy\Contracts\QuPolicyInterface;
use App\Repositories\Ota\Tc\Policy\Contracts\TcPolicyInterface;
use Illuminate\Http\Request;

class CompareController
{

    private $specialInterface;
    private $tcPolicyInterface;
    private $qnPolicyInterface;
    private $policyUploadInterface;
    public function __construct(SpecialPolicyInterface $specialInterface,TcPolicyInterface $tcPolicyInterface,QuPolicyInterface $qnPolicyInterface,PolicyUploadInterface $policyUploadInterface)
    {
        $this->specialInterface = $specialInterface;
        $this->tcPolicyInterface = $tcPolicyInterface;
        $this->qnPolicyInterface = $qnPolicyInterface;
        $this->policyUploadInterface = $policyUploadInterface;
    }

    /**
     * 携程比价
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Foundation\Application|\Illuminate\View\View
     */
    public function ctrip(){

        return view('compare.ctrip');
    }

    /**
     * 同程比价
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Foundation\Application|\Illuminate\View\View
     */
    public function tc(){

        return view('compare.tc');
    }

    /**
     * 去哪比价
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Foundation\Application|\Illuminate\View\View
     */
    public function qn(){

        return view('compare.qn');
    }

    /**
     * 判断政策是否存在
     * @param $departCode
     * @param $arrivedCode
     * @param $departAt
     * @param $adminId
     * @return mixed
     */
    public function policyUploadExist($departCode,$arrivedCode,$departAt,$adminId){
        $policyWhere = [
            'depart_city_code'      => $departCode,
            'arrived_city_code'     => $arrivedCode,
            'depart_at'             => $departAt,
            'status'                => 1,
            'admin_id'              => $adminId,
        ];

        return $this->policyUploadInterface->index($policyWhere);
    }

    /**
     * 携程比价
     * @param Request $request
     * @return mixed
     */
    public function ajaxCtrip(Request $request){

        if ($request->EffectDate && $request->ExipryDate && $request->DepartPort && $request->ArrivePort) {

            if ($this->policyUploadExist($request->DepartPort,$request->ArrivePort,$request->EffectDate,$request->admin_id)){

                $data = $request->all();
                $data['ShareProductTypeList'] = explode(',',$request->ShareProductTypeList);
                if ($request->platform && ($request->platform == 10 || $request->platform == 20)) {
                    $res = $this->specialInterface->lowPricePolicyList($data);
                } elseif ($request->platform && ($request->platform == 11 || $request->platform == 21)) {
                    $res = $this->specialInterface->lowPricePolicyMhList($data);
                } elseif(in_array(10,request()->admin_platform) || in_array(20,request()->admin_platform)){
                    $res = $this->specialInterface->lowPricePolicyList($data);
                }  elseif(in_array(11,request()->admin_platform) || in_array(21,request()->admin_platform)){
                    $res = $this->specialInterface->lowPricePolicyMhList($data);
                }

                if ($res){

                    if (isset($res['TradeResult']) && is_array($res['TradeResult']) && isset($res['TradeResult']['PolicyList'])) {


                        return new BaseResource($res['TradeResult']['PolicyList']);
                    }

                    return new ErrorResource($res['TradeResult']['ErrorList'][0]['ErrorMessage'] );
                } else {

                    return new ErrorResource('NETWORK_ERROR');
                }
            } else {

                return new ErrorResource('POLICY_NOT_EXIST');
            }


        }

        return new BaseResource([]);

    }

    /**
     * 去哪比价
     * @param Request $request
     * @return mixed
     */
    public function ajaxQn(Request $request){

        if ($request->flightDate && $request->dpt && $request->arr) {
            if ($this->policyUploadExist($request->dpt, $request->arr, $request->flightDate, $request->admin_id)) {
                $res = $this->qnPolicyInterface->lowPricePolicyList($request->all());

                if ($res) {

                    if (isset($res['code']) && $res['code'] == 0) {

                        if (!empty($res['result']['data'])) {

                            return new BaseResource($res['result']['data']['orderList']);
                        }

                    }

                    return new ErrorResource(['errmsg' => $res['result']['errmsg'], 'errcode' => 1]);
                } else {

                    return new ErrorResource('NETWORK_ERROR');
                }
            } else {
                return new ErrorResource('POLICY_NOT_EXIST');
            }
        }

        return new BaseResource([]);

    }

    /**
     * 同程政策
     * @param Request $request
     * @return mixed
     */
    public function ajaxTc(Request $request)
    {

        if ($request->startPort && $request->endPort &&  $request->startDate){

            if ($this->policyUploadExist($request->startPort, $request->endPort, $request->startDate, $request->admin_id)) {
                $data = [
                    'airlineCode' => $request->airlineCode,
                    'startPort' => $request->startPort,
                    'endPort' => $request->endPort,
                    'reimbursementType' => $request->reimbursementType,
                    'shareType' => $request->shareType,
                ];

                if ($request->endDate) {

                    $data['flyDateRange'] = [
                        'startDate' => $request->startDate,
                        'endDate' => $request->endDate,
                    ];

                } else {

                    $data['flyDate'] = $request->startDate;
                }

                if ($request->flightNo) {

                    $data['flightNo'] = $request->flightNo;
                }

                $res = $this->tcPolicyInterface->lowPricePolicyList($data);
                //格式化数据
                if (!empty($res)) {

                    //匹配有效数据
                    return new BaseResource($res);
                }

                return new ErrorResource('NETWORK_ERROR');

            } else {
                return new ErrorResource('POLICY_NOT_EXIST');
            }
        }
        return new BaseResource([]);
    }
}